package com.yaodian100.ec.product.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;

import com.yaodian100.core.common.dao.BaseDAO;
import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.common.service.impl.DomainServiceImpl;
import com.yaodian100.ec.product.entity.AndeProduct;
import com.yaodian100.ec.product.service.AndeProductService;

/**
 * 
 * @author apple.chang
 * 
 */
public class AndeProductServiceImpl extends DomainServiceImpl<AndeProduct> implements AndeProductService {
	@Resource
	private BaseDAO baseDAO;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.ec.product.service.DaphneProductService#importDaphneProduct(java.lang.String, java.lang.String)
	 */
	@Override
	@Transactional(readOnly = false)
	public boolean importAndeProduct(String importDate, String[] productId) {
		try {
			// 匯入資料前刪除全部的資料
			baseDAO.executeSql("truncate table EC_ANDE_PRODUCT drop storage", null);

			List<AndeProduct> andeProducts = new ArrayList<AndeProduct>();
			AndeProduct ande = null;
			for (int i = 0; i < productId.length; i++) {
				ande = new AndeProduct();
				ande.setProductId(productId[i]);
				andeProducts.add(ande);
			}
			if (!andeProducts.isEmpty()) {
				getDao().saveOrUpdateBatch(andeProducts);
			}
			System.out.println("andeProducts{}:" + andeProducts);
		} catch (Exception e) {
			logger.info("exception:{}", e.fillInStackTrace());
			return false;
		}
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.ec.product.service.AndeProductService#isAndeProduct(java.lang.String)
	 */
	@Override
	public boolean hasAndeProduct(List<String> productIds) {
		CommonCriteria crit = new CommonCriteria();
		crit.addIn("productId", productIds);
		List<AndeProduct> pids = getDao().getList(crit, null);
		if (null != pids && !pids.isEmpty()) {
			return true;
		}
		return false;
	}

}